﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
  /*添加3个类，分别实现 IComparer<T>接口，实现对Student类的三个字段的排序。
    1、学生类：学号、姓名、年龄
    2、请选择：1、添加学生信息。2、查询学生信息，3、删除学生信息。
    3、重复的学号不能添加。
    4、查询学生信息功能中有：
    1、查询所有（按学号排序）2、查询所有（按姓名排序），3、查询所有（按年龄排序）
    4、按学号查询（查没有，则打印查无此学生）5、退出*/
    class Program
    {
        public static Dictionary<int, Student> list = new Dictionary<int, Student>();
        static void Main(string[] args)
        {
            Student s1 = new Student( "张三", 19 ,1);
            Student s2 = new Student( "李四", 16 ,2);
            Student s3 = new Student( "王五", 25 ,3);
            list.Add(1, s1);
            list.Add(2, s2);
            list.Add(3, s3);
            while (true)
            {
                Console.WriteLine("1.添加学生信息    2.查询学生信息   3.删除学习信息");
                string str = Console.ReadLine();
                switch (str)
                {
                    case "1":
                        Addition();
                        break;
                    case "2":
                        Inquire();
                        break;
                    case "3":
                        Delete();
                        break;
                    default:
                        break;
                }
            }
            Console.ReadKey();
        }
        static void Addition() 
        {
            Console.WriteLine("请输入学生的学号:");
            int c = int.Parse(Console.ReadLine());
            for (int i = 0; i < list.Count; i++)
            {
                if (list.ContainsKey(c))
                {
                    Console.WriteLine("学号已存在，请重新输入");
                    Addition();
                }
            }
            Console.WriteLine("请输入学生的姓名:");
            string a = Console.ReadLine();
            Console.WriteLine("请输入学生的年龄:");
            int b = int.Parse(Console.ReadLine());
            Student student = new Student(a,b,c);
            list.Add(student.num,student);
            Console.WriteLine("添加成功");
        }
        static void Inquire() 
        {
            while (true)
            {
                Console.WriteLine("1.查询所有(按学号排序)    2.查询所有(按姓名排序)    3.查询所有(按年龄排序)    4.按学号查询(查没有,则打印查无此学生)    5.退出 ");
                string str = Console.ReadLine();
                switch (str)
                {
                    case "1":
                        InquireNum();
                        break;
                    case "2":
                        InquireName();
                        break;
                    case "3":
                        InquireAge();
                        break;
                    case "4":
                        InquireID();
                        break;
                    case "5":
                        return;
                    default:
                        break;
                }
            }
        }
        static void InquireNum() 
        {
            List<Student> alist = new List<Student>();
            foreach (var value in list.Values)
            {
                alist.Add(value);
            }
            alist.Sort(new SortNum());
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
        }
        static void InquireName()
        {
            List<Student> alist = new List<Student>();
            foreach (var value in list.Values)
            {
                alist.Add(value);
            }
            alist.Sort(new SortName());
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
        }
        static void InquireAge()
        {
            List<Student> alist = new List<Student>();
            foreach (var value in list.Values)
            {
                alist.Add(value);
            }
            alist.Sort(new SortAge());
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
        }
        static void InquireID() 
        {
            Console.WriteLine("请输入要查询学生的学号:");
            int num = int.Parse(Console.ReadLine());
            for (int i = 0; i < list.Count; i++)
            {
                if (list.ContainsKey(num))
                {
                    Console.WriteLine(list[num]);
                    break;
                }
                else
                {
                    Console.WriteLine("未找到所查询学生学号的信息");
                }
            }
        }
        static void Delete() 
        {
            Console.WriteLine("请输入想要删除学生信息的学号:");
            int num = int.Parse(Console.ReadLine());
            for (int i = 0; i < list.Count; i++)
            {
                if (list.ContainsKey(num))
                {
                    list.Remove(num);
                }
            }
            Console.WriteLine("删除成功");
        }
    }
}
